package reduce.fz.com.reduce.fragment;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.WitnessCardReplyActivity;
import reduce.fz.com.reduce.adapter.WitnessCardAdapter;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.TeamDynamic;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.WitnessCard;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.bean.result.UserWitness;
import reduce.fz.com.reduce.constant.ParamConst;
import reduce.fz.com.reduce.custom.JazzyViewPager;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.event.PubDynamicEvent;
import reduce.fz.com.reduce.event.WitnessReplyEvent;
import reduce.fz.com.reduce.fragment.base.BaseFragment;
import reduce.fz.com.reduce.service.WitnessService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.TeamDynamicUtil;
import reduce.fz.com.reduce.util.ToastUtil;

/**
 * 见证首页
 */
public class WitnessFragment extends BaseFragment {
    //横滑的viewPager
    private JazzyViewPager mJazzy;
    private Long teamid;
    private Pagination pagination = new Pagination();
    //小组内待见证列表
    private List<WitnessCard> witnessCurrent = new ArrayList<WitnessCard>();
    private LayoutInflater layoutInflater;
    private WitnessService witnessService = ServiceBuilder.builderWitnessService(null);
    private WitnessCardAdapter.WitnessCardListener cardListener;
    private long startnum = -1;
    private WitnessCardAdapter cardAdapter;
    private Boolean hasLoading = false;
    private LoadingDialog dialog;

    public WitnessFragment() {
    }

    public static WitnessFragment create(Long teamid) {
        Bundle bundle = new Bundle();
        bundle.putLong("teamid", teamid);
        WitnessFragment fragment = new WitnessFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        teamid = getArguments().getLong("teamid");
        EventBus.getDefault().register(this);
    }

    public void onEvent(WitnessReplyEvent event)
    {
       WitnessCard card= witnessCurrent.get(mJazzy.getCurrentItem());
        if(card==null) return;
        card.witnessAttach.reply_count++;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void loadMore(Boolean displyLoading) {
        if (displyLoading) dialog.show();
        witnessService.loadWitnessDynamic(teamid, pagination.getPageNo(), pagination.getPageSize(), new AbResponseListener<List<WitnessCard>>(new TypeReference<Result<List<WitnessCard>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();

            }

            @Override
            public void responseInfo(Result<List<WitnessCard>> data) {
                pagination.nextPage();
                dialog.dismiss();
                if (data.getData() != null && !data.getData().isEmpty()) {
                    startnum = data.getData().get(data.getData().size() - 1).cardid;
                    cardAdapter.addCard(data.getData());


                }

            }
        });
    }

    /**
     * 初始化监听
     */
    public void initCardListener() {
        cardListener = new WitnessCardAdapter.WitnessCardListener() {
            @Override
            public void onWitnessNo(WitnessCard card) {

                witnessService.witnessCard(card.cardid, ParamConst.WITNESS_NO, new AbResponseListener(new TypeReference<Result>() {
                }) {
                    @Override
                    public void error(ServiceException e) {

                    }

                    @Override
                    public void responseInfo(Result data) {
                        ToastUtil.showMessage("见证不通过!");
                    }
                });
            }

            @Override
            public void onWitnessComments(WitnessCard card) {
                forwardComments(card);

            }

            @Override
            public void onWitnessYes(final WitnessCard card) {
                witnessService.witnessCard(card.cardid, ParamConst.WITNESS_YES, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
                }) {
                    @Override
                    public void error(ServiceException e) {
                    }

                    @Override
                    public void responseInfo(Result<HashMap> data) {
                        ToastUtil.showMessage("见证成功!");
                        User user = UserPreference.getUser();
                        // TeamDynamic teamDynamic= TeamDynamicUtil.witness(user,card.user);
                        //EventBus.getDefault().post(new PubDynamicEvent(teamDynamic));
                    }
                });

            }
        };
    }

    /**
     * 跳转到comments评论列表
     */

    public void forwardComments(WitnessCard card) {
        Intent intent = new Intent(getActivity(), WitnessCardReplyActivity.class);
        intent.putExtra("cardid", card.cardid);
        startActivity(intent);
    }





    private void setupJazziness(JazzyViewPager.TransitionEffect effect, View view) {
        mJazzy = (JazzyViewPager) view.findViewById(R.id.jazzy_pager);
        mJazzy.setTransitionEffect(effect);
        cardAdapter = new WitnessCardAdapter(getActivity(), witnessCurrent, cardListener, mJazzy);
        mJazzy.setAdapter(cardAdapter);
        mJazzy.setPageMargin(30);
        mJazzy.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position > cardAdapter.userWitness.size() - 4) {
                    loadMore(false);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        initCardListener();
        dialog = new LoadingDialog(getActivity());
        View view = inflater.inflate(R.layout.fragment_witness, container, false);
        setupJazziness(JazzyViewPager.TransitionEffect.Tablet, view);
        loadMore(true);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        layoutInflater = getActivity().getLayoutInflater();

    }


}
